package com.sec.android.app.myfiles.module.local.file;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.DragAndDropMgr;
import com.sec.android.app.myfiles.fragment.filelist.FileListAdapter;
import com.sec.android.app.myfiles.fragment.home.HomeFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.foldertree.FolderTreeRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.util.StorageMonitor;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;

import java.util.HashMap;

public class LocalFileTabletHomeItem extends LocalFileHomeItem implements AdapterView.OnItemClickListener {

    private SparseArray<AbsListViewImp> mListViewMap;
    private HashMap<AbsListView, FileListAdapter> mListAdapterMap;

    public LocalFileTabletHomeItem(Context context, HomeFragment fragment) {
        super(context, fragment);
    }

    @Override
    protected void setListItem() {
        if (mListViewMap == null) {
            mListViewMap = new SparseArray<>();
        }
        if (mListAdapterMap == null) {
            mListAdapterMap = new HashMap<>();
        }
        super.setListItem();
    }

    @Override
    protected boolean checkDeviceStorage() {
        boolean bRet = super.checkDeviceStorage();
        if (bRet) {
            setFolderTreeView(R.id.device_storage, AppConstants.StoragePath.INTERNAL_ROOT, mListViewMap, mListAdapterMap);
        }
        return bRet;
    }

    @Override
    protected boolean checkSdCard(int stubId, int viewId, int iconId, int stringId) {
        boolean bRet = super.checkSdCard(0, viewId, iconId, stringId);
        if (bRet) {
            if (mListViewMap.get(R.id.sd_card) == null) {
                setFolderTreeView(R.id.sd_card, StorageMonitor.getExtSDCardPath(), mListViewMap, mListAdapterMap);
            }
        } else {
            removeStorageInfo(R.id.sd_card);
        }
        return bRet;
    }

    @Override
    protected boolean checkPrivateMode() {
        boolean bRet = super.checkPrivateMode();
        if (bRet) {

        } else {
            removeStorageInfo(R.id.private_storage);
        }
        return bRet;
    }

    @Override
    protected boolean checkUsbStorage() {
        return super.checkUsbStorage();
    }

    private void setFolderTreeView(int resId, String path, SparseArray<AbsListViewImp> listViewMap, HashMap<AbsListView, FileListAdapter> adapterMap) {
        View view = mFragment.getView().findViewById(resId);
        NavigationInfo folderTreeInfo;

        FolderTreeRecord record = new FolderTreeRecord(-1, FileRecord.getPath(path), FileRecord.getName(path), 0, 0, FileType.FOLDER, 0, 1, 1);
        folderTreeInfo = NavigationInfo.getInstance(NavigationMode.Normal, record);
        folderTreeInfo.setCurFragment(mFragment);

        AbsListViewImp oldListViewImp = listViewMap.get(resId);
        if (oldListViewImp != null) {
            AbsListView oldListView = oldListViewImp.getListView();
            if (adapterMap.containsKey(oldListView)) {
                adapterMap.remove(oldListView);
            }
        }

        AbsListViewImp listView = AbsListViewImp.createListView(mContext, view, folderTreeInfo);
        listViewMap.put(resId, listView);

        FileListAdapter adapter = new FileListAdapter(mContext, folderTreeInfo, mFragment.getLoaderManager(), listView);
        adapterMap.put(listView.getListView(), adapter);

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        DragAndDropMgr dndMgr = new DragAndDropMgr(mFragment, FileRecord.StorageType.FolderTree, listView);
        dndMgr.attachDragListenerToView(listView.getListView());

        View storageOpen = view.findViewById(R.id.device_storage_expand_indicator);
        record.setOpened(true);
        MyFilesFacade.openFolderTree(mFragment.getProcessId(), mContext, record);

        storageOpen.setTag(record);
        storageOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expandStorage(v);
            }
        });
    }

    private void removeStorageInfo(int resId) {
        AbsListViewImp listViewImp = mListViewMap.get(resId);
        if (listViewImp != null) {
            mListAdapterMap.remove(listViewImp.getListView());
            mListViewMap.remove(resId);
        }
    }

    public void expandStorage(View v) {
        FolderTreeRecord record = (FolderTreeRecord) v.getTag();
        if (record != null) {
            Log.d(this, "indicator :" + record.getFullPath());
            MyFilesFacade.openFolderTree(mFragment.getProcessId(), mContext, record);

            if (record.isOpened()) {
                if (v.getRotation() == -180) {
                    v.animate().rotation(0).setDuration(300);
                }
            } else {
                if (v.getRotation() == 0) {
                    v.animate().rotation(-180).setDuration(300);
                }
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (view != null && UiUtils.isValidClick(view.getId())) {
            FileListAdapter adapter = mListAdapterMap.get(parent);
            if (adapter != null) {
                FileRecord record = adapter.getFileRecord(position);
                if (record != null) {
                    record = new LocalFileRecord(record.getFullPath());
                    MyFilesFacade.executeRecord(mFragment.getProcessId(), mFragment.getActivity(), record);
                }
            }
        }
    }
}
